package com.sc.VenusQueryBeta.presenter.search;

import com.sc.VenusQueryBeta.contract.search.SearchContract;
import com.sc.VenusQueryBeta.model.bean.home.KeyWordListBean;
import com.sc.VenusQueryBeta.model.bean.home.SearchListBean;
import com.sc.VenusQueryBeta.model.search.SearchModel;

import io.reactivex.functions.Consumer;

/**
 * 搜索presenter 继承契约类的presenper
 */

public class SearchPresenter extends SearchContract.SearchPresenter {
    @Override
    protected SearchContract.ISearchModel getModel() {
        return SearchModel.newInstance();
    }

    @Override
    public void onStart() {

    }

    /**
     * 实例化对象
     */
    public static SearchPresenter newInstance() {
        return new SearchPresenter();
    }

    /**
     * 獲取keyword列表
     */
    @Override
    public void getKeyWordList() {
        if (mIView == null || mIModel == null)
            return;
        mRxManager.register(mIModel.getKeyWordList().subscribe(new Consumer<KeyWordListBean>() {
            @Override
            public void accept(KeyWordListBean keyWordListBean) throws Exception {
                if (mIView != null)
                    mIView.KeyWordListBeanDataSuccess(keyWordListBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mIView.showToast("Network error.");
                mIView.showNetworkError();
            }
    }));
}


    /**
     * 搜索
     */
    @Override
    public void search(String keyWord) {
        if (mIView == null || mIModel == null)
            return;
        mRxManager.register(mIModel.search(keyWord).subscribe(new Consumer<SearchListBean>() {
            @Override
            public void accept(SearchListBean searchListBean) throws Exception {
                if (mIView != null)
                    mIView.getSearchDataSuccess(searchListBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mIView.showToast("Network error.");
                mIView.showNetworkError();
            }
        }));
    }
}
